{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 字符串操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们一起回归一下python字符串的相关操作，这是非常基础的知识，但却是使用频度非常高的一些功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 去空格及特殊符号 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n",
      "world!\n",
      " hello, world\n"
     ]
    }
   ],
   "source": [
    "s = ' hello, world!'\n",
    "print s.strip()\n",
    "print s.lstrip(' hello, ')\n",
    "print s.rstrip('!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 连接字符串 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "strcatappend\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'strcat'\n",
    "sStr2 = 'append'\n",
    "sStr1 += sStr2\n",
    "print sStr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 查找字符 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "# < 0 为未找到\n",
    "sStr1 = 'strchr'\n",
    "sStr2 = 'r'\n",
    "nPos = sStr1.index(sStr2)\n",
    "print nPos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 比较字符串 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-1\n",
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'strchr'\n",
    "sStr2 = 'strch'\n",
    "print cmp(sStr2, sStr1)\n",
    "print cmp(sStr1, sStr2)\n",
    "print cmp(sStr1, sStr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 字符串中的大小写转换 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jcstrlwr\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'JCstrlwr'\n",
    "#sStr1 = sStr1.upper()\n",
    "sStr1 = sStr1.lower()\n",
    "print sStr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 翻转字符串 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gfedcba\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'abcdefg'\n",
    "sStr1 = sStr1[::-1]\n",
    "print sStr1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 查找字符串 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'abcdefg'\n",
    "sStr2 = 'cde'\n",
    "print sStr1.find(sStr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 分割字符串 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cde,fgh,ijk\n",
      "['ab', 'cde', 'fgh', 'ijk']\n"
     ]
    }
   ],
   "source": [
    "sStr1 = 'ab,cde,fgh,ijk'\n",
    "sStr2 = ','\n",
    "sStr1 = sStr1[sStr1.find(sStr2) + 1:]\n",
    "print sStr1\n",
    "#或者\n",
    "s = 'ab,cde,fgh,ijk'\n",
    "print(s.split(','))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "** 计算字符串中出现频次最多的字幕 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#version 1\n",
    "import re\n",
    "from collections import Counter\n",
    "\n",
    "def get_max_value_v1(text):\n",
    "    text = text.lower()\n",
    "    result = re.findall('[a-zA-Z]',text) # 去掉列表中的符号符\n",
    "    count = Counter(result)  # Counter({'l': 3, 'o': 2, 'd': 1, 'h': 1, 'r': 1, 'e': 1, 'w': 1})\n",
    "    count_list = list(count.values())\n",
    "    max_value = max(count_list)\n",
    "    max_list = []\n",
    "    for k,v in count.items():\n",
    "        if v == max_value:\n",
    "            max_list.append(k)\n",
    "    max_list = sorted(max_list)\n",
    "    return max_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#version 2\n",
    "from collections import Counter\n",
    "\n",
    "def get_max_value(text):\n",
    "    count = Counter([x for x in text.lower() if x.isalpha()])\n",
    "    m = max(count.values())\n",
    "    return sorted([x for (x,y) in count.items() if y == m])[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#version 3\n",
    "import string\n",
    "\n",
    "def get_max_value(text):\n",
    "    text = text.lower()\n",
    "    return max(string.ascii_lowercase, key=text.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'l'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(range(6),key = lambda x : x>2)\n",
    "# >>> 3\n",
    "# 带入key函数中，各个元素返回布尔值，相当于[False, False, False, True, True, True]\n",
    "# key函数要求返回值为True，有多个符合的值，则挑选第一个。\n",
    "\n",
    "max([3, 5, 2, 1, 4, 3, 0], key = lambda x : x)\n",
    "# >>> 5\n",
    "# 带入key函数中，各个元素返回自身的值，最大的值为5，返回5.\n",
    "\n",
    "max('ah', 'bf', key = lambda x : x[1])\n",
    "# >>> 'ah'\n",
    "# 带入key函数，各个字符串返回最后一个字符，其中'ah'的h要大于'bf'中的f，因此返回'ah'\n",
    "\n",
    "max('ah', 'bf', key = lambda x : x[0])\n",
    "# >>> 'bf'\n",
    "# 带入key函数，各个字符串返回第一个字符，其中'bf'的b要大于'ah'中的a，因此返回'bf'\n",
    "\n",
    "text = 'Hello World'\n",
    "max('abcdefghijklmnopqrstuvwxyz', key = text.count)\n",
    "# >>> 'l'\n",
    "# 带入key函数，返回各个字符在'Hello World'中出现的次数，出现次数最多的字符为'l',因此输出'l'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Count occurrence of a character in a Python string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "#T  h  e     M  i  s  s  i  s  s  i  p  p  i     R  i  v  e  r\n",
    "#[1, 1, 2, 2, 1, 5, 4, 4, 5, 4, 4, 5, 2, 2, 5, 2, 1, 5, 1, 2, 1]\n",
    "sentence = 'The Mississippi River'\n",
    "\n",
    "def count_chars(s):\n",
    "    s = s.lower()\n",
    "    count = list(map(s.count,s))\n",
    "    return (max(count))\n",
    "\n",
    "print count_chars(sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
